function fastSearchOutPatient(resultContainerId, dialogContainerId) {
    var searchString = $.trim($('#quickSearch').val());
    if (searchString == '') {
        return;
    }   
    var numbers = searchString.match(/\d+\.?\d*/g);
    if (numbers == null) {
        searchOutPatientByName(searchString, resultContainerId, dialogContainerId);
        
    } else {
        searchOutPatientByCode(searchString, resultContainerId);
    }
}

function searchOutPatientByCode(patientCode, resultContainerId) {
   var patientCode = pad(patientCode,-6,'0');
   $('#quickSearch').val(_("Searching..."));   
   $.ajax ({
       type:     "POST",
       url:      "../TicketFinder/searchOutPatientByCode?set_ajax_view",
       data:     "patientCode=" + patientCode,           
       dataType: "json",
       complete: function() {
            showAjaxLoader('ajaxLoader', 'dummy', false);
       },
       beforeSend: function() {
            showAjaxLoader('ajaxLoader', 'dummy', true);
        },
       success:  function(data) {
           $('#quickSearch').val('');
           if (data.error.code == 'OK') {
        	   setDataToElements(resultContainerId, data.result);               
           } else {                                   
               var buttons = {};
               buttons[_('Close')] = function() {
                   $(this).dialog('close');
               };
               generalDialog(null, _('Search patient'), data.error.mess, 'error', {
                   autoOpen: true,         
                   bgiframe: true,
                   resizable: false,
                   modal: true,
                   width: 400,
                   height: 100,
                   buttons: buttons,
                   close: function(event, ui) {
                        $('#quickSearch').val('').focus();
                   }}, _('Close'));
           }
       }
   });
}

function searchOutPatientByName(patientName, resultContainerId, dialogContainerId) {
    $('#quickSearch').val(_("Searching..."));
    $.ajax ({
        type:     "POST",
        url:      "../TicketFinder/searchOutPatientByName?set_ajax_view",
        data:     {
            patientName:    patientName,
            dialogId:       dialogContainerId,
            jsFunction:      'viewOutPaymentDetails'
        },        
        dataType: "json",
        complete: function() {
            showAjaxLoader('ajaxLoader', 'dummy', false);
       },
       beforeSend: function() {
            showAjaxLoader('ajaxLoader', 'dummy', true);
        },
        success:  function(data) {
            if (data.error.code == 'OK') {
                $('#quickSearch').val('');
                if (data.result.type == 'final') {    
                    setDataToElements(resultContainerId, data.result);
                }
                else if (data.result.type == 'selection') {
                    var buttons = {};
                    buttons[_('Close')] = function() {
                        $(this).dialog('close');
                    };
                    generalDialog(dialogContainerId, _('Search patient'), data.result.html, null, {
				        autoOpen: true,         
				        bgiframe: true,
				        resizable: true,                
				        width: 850,
				        height: 450,
				        modal: true,
				        buttons: buttons,
				        close: function(event, ui) {
                            $('#quickSearch').val('').focus();
                        }
				    }, _('Close')); 
                }
                
            } else {       
                var buttons = {};
                buttons[_('Close')] = function() {
                    $(this).dialog('close');
                };
				generalDialog(null, _('Search patient'), data.error.mess, 'error', {
				    autoOpen: true,         
				    bgiframe: true,
				    resizable: false,
				    modal: true,
				    width: 400,
                    height: 100,
				    buttons: buttons,
				    close: function(event, ui) {
				        $('#quickSearch').val('').focus();
				    }}, _('Close'));     

                }
            }
        });
}

function setDataToElements(containers, data) {
    if (data instanceof Object) {
	    for (var x in data) {
		    if ($('#' + containers[x]).length > 0 ) {
		        
		        setElementData(containers[x], data[x]);
		    }
	    }
    }
    else {
        setElementData(containers, data);
    }     
}

function fastSearchInsurancePatient(resultContainerId, dialogContainerId) {
    var searchString = $.trim($('#quickSearch').val());
    if (searchString == '') {
        return;
    }   
    var numbers = searchString.match(/\d+\.?\d*/g);
    if (numbers == null) {
    	searchInsurancePatientByName(searchString, resultContainerId, dialogContainerId);
        
    } else {
    	searchInsurancePatientByCode(searchString, resultContainerId);
    }
}

function searchInsurancePatientByCode(patientCode, resultContainerId) {
   var patientCode = pad(patientCode,-6,'0');
   $('#quickSearch').val(_("Searching..."));   
   $.ajax ({
       type:     "POST",
       url:      "../TicketFinder/searchInsurancePatientByCode?set_ajax_view",
       data:     "patientCode=" + patientCode,           
       dataType: "json",
       complete: function() {
            showAjaxLoader('ajaxLoader', 'dummy', false);
       },
       beforeSend: function() {
            showAjaxLoader('ajaxLoader', 'dummy', true);
        },
       success:  function(data) {
           $('#quickSearch').val('');
           if (data.error.code == 'OK') {
               setDataToElements(resultContainerId, data.result);               
           } else {                                   
               var buttons = {};
               buttons[_('Close')] = function() {
                   $(this).dialog('close');
               };
               generalDialog(null, _('Search patient'), data.error.mess, 'error', {
                   autoOpen: true,         
                   bgiframe: true,
                   resizable: false,
                   modal: true,
                   width: 400,
                   height: 100,
                   buttons: buttons,
                   close: function(event, ui) {
                        $('#quickSearch').val('').focus();
                   }}, _('Close'));
           }
       }
   });
}

function searchInsurancePatientByName(patientName, resultContainerId, dialogContainerId) {
    $('#quickSearch').val(_("Searching..."));
    $.ajax ({
        type:     "POST",
        url:      "../TicketFinder/searchInsurancePatientByName?set_ajax_view",
        data:     {
            patientName:    patientName,
            dialogId:       dialogContainerId,
            jsFunction:      'viewInsurancePaymentDetails'
        },           
        dataType: "json",
        complete: function() {
            showAjaxLoader('ajaxLoader', 'dummy', false);
        },
        beforeSend: function() {
            showAjaxLoader('ajaxLoader', 'dummy', true);
        },
        success:  function(data) {
            if (data.error.code == 'OK') {
                $('#quickSearch').val('');
                if (data.result.type == 'final') {
                    setDataToElements(resultContainerId, data.result);
                }
                else if (data.result.type == 'selection') {
                    var buttons = {};
                    buttons[_('Close')] = function() {
                        $(this).dialog('close');
                    };
                    generalDialog(dialogContainerId, _('Search patient'), data.result.html, null, {
                        autoOpen: true,         
                        bgiframe: true,
                        resizable: true,                
                        width: 850,
                        height: 450,
                        modal: true,
                        buttons: buttons,
                        close: function(event, ui) {
                            $('#quickSearch').val('').focus();
                        }
                    }, _('Close')); 
                }
                
            } else {       
                var buttons = {};
                buttons[_('Close')] = function() {
                    $(this).dialog('close');
                };
                generalDialog(null, _('Search patient'), data.error.mess, 'error', {
                    autoOpen: true,         
                    bgiframe: true,
                    resizable: false,
                    modal: true,
                    width: 400,
                    height: 100,
                    buttons: buttons,
                    close: function(event, ui) {
                        $('#quickSearch').val('').focus();
                    }}, _('Close'));     

                }
            }
        });
}

function showCustomDialog(message, fieldId2Focus) {
    var buttons = {};
    buttons[_('Close')] = function() {
        $(this).dialog('close');            
        $('#' + fieldId2Focus).focus();
    };
    
    generalDialog(null, _('Error'), message, 'error', {
        autoOpen: true,         
        bgiframe: true,
        resizable: false,
        modal: true,
        buttons: buttons,
        dialogClass: 'custom-dialog'
    }, _('Close'));
}

function viewAdvancePaymentDetails(ticketId) {
    $.ajax ({   
        type:       "POST", 
        url:        "../PaymentCommon/viewAdvancePaymentDetails?set_ajax_view",
        data:       "ticketId=" + ticketId, 
        dataType:   "json",
        beforeSend: function() {
            $('#ajaxLoader').show();
        },
        complete:   function() {                        
            $('#ajaxLoader').hide();
        },      
        success:    function(data) {
            if (data.error.code == 'OK') {
                var buttons = {};
                buttons[_('Close')] = function() {
                    $(this).dialog('close');        
                };
                var title   =   data.result.ticketName + ": " + _('Real payment details');
                generalDialog('dialogDiv', title,  data.result.html, null, {
                    autoOpen: true,         
                    bgiframe: true,
                    resizable: false,
                    modal: true,
                    width: 840,
                    height: 350,
                    buttons: buttons
                }, _('Close'));
            }
            else {
                displayError(data.error.mess);              
            }                                                                                  
        }
    });     
}   